Embedded Programming


Home

Overview of week 4

Group Assignment

Demonstrate and compare the toolchains and development workflows for available embedded architectures.

Individual Assignments

Browse through the data sheet for our microcontroller write a program for a microcontroller, and simulate its operation, to interact (with local input &/or output devices) and communicate (with remote wired or wireless connections).

Group Assignment :

Link


For the group assigment I used the XIAO-ESP32-C3 board and one LED and resistor, by using the VS code I made the LED blink.


I learnt about how to do the code compiling and uploading on the various microcontrollers


Individual Assignment :

Arduino

Arduino is an open-source electronics platform with hardware and software designed for ease of use. It features microcontroller-based boards like Arduino Uno (ATmega328P) and supports C/C++ programming. The Arduino IDE simplifies coding and uploading programs. It interfaces with sensors, LEDs, motors, and other components. Its open-source nature allows customization and improvements. With extensive community support, it is widely used in education, prototyping, and IoT projects.

In my exploration, I am using the Arduino Uno, which has allowed me to learn basic simulations in software platforms.

Image source


ATmega328P

The ATmega328P is an 8-bit AVR microcontroller developed by Microchip Technology. It is widely used in embedded systems.

Specification

  • Architecture: 8-bit AVR RISC
  • Clock Speed: Up to 20 MHz (16 MHz in Arduino Uno)
  • Flash Memory: 32 KB (for storing programs)
  • EEPROM: 1 KB (for non-volatile data storage)
  • GPIO Pins: 23 I/O pins (including 6 PWM and 6 ADC channels)
  • Communication Interfaces: UART, SPI, I2C

Datasheet

I first reviewed the table of contents of the datasheet to understand the document structure. I observed sections related to pin configuration, memory organization, timers, ADC, communication protocols, interrupts, electrical characteristics, and power management.
It helps quickly locate information during hardware design and debugging.

Block diagram

Pin Configuration

I explored the pinout diagram and identified:
  • Digital I/O pins
  • Analog input pins
  • UART pins
  • SPI pins
  • I2C pins
  • Power pins
  • When connecting any sensors, displays, motors and communication modules these all will useful to us.

    Image Source

    AVR Memories

    I checked the memory section,
    I learned that:
  • Flash Memory stores the program code uploaded to the microcontroller.
  • SRAM stores temporary variables while the program is running.
  • EEPROM stores data permanently even after power is turned OFF.
  • It helps us to understand memory limitations while programming.
    For instant if I create a project to control an LED using a push button,
  • The Arduino code will stored in Flash Memory.
  • When the program runs, variables such as buttonState and ledState are stored in SRAM.
  • If I want to remember how many times the button was pressed even after power is disconnected, I can store that count in EEPROM.
  • It helps me understand where the program, temporary data, and permanent data are stored inside the microcontroller and how memory limitations affect embedded programming.

    Electrical Characteristics

    I explored:
  • Operating voltage
  • Current limits
  • Logic HIGH and LOW levels
  • While using the Arduino Uno, I learned that the ATmega328P typically operates at 5V. When connecting an LED, I cannot connect it directly to the microcontroller pin because excessive current may damage the pin. Therefore, I use the resistor to limit the current flowing through the LED.
    I also learned that the microcontroller interprets voltages close to 5V as HIGH (logic 1) and voltages close to 0V as LOW (logic 0). This is how the microcontroller determines whether a button is pressed or an LED should be turned ON or OFF.
    Understanding the electrical characteristics helps me select appropriate components, design safe circuits, and prevent damage to the microcontroller during hardware development and testing.

    ADC Section

    I explored the ADC chapter and found:
  • 10-bit resolution
  • Multiple analog channels
  • Reference voltage options
  • An ADC converts an analog voltage into a digital value that the microcontroller can process. For a 10-bit ADC, the output value ranges from 0 to 1023.
    For instant
  • 0V → Digital value 0
  • 2.5V → Digital value approximately 512
  • V → Digital value approximately 1023
  • For reading sensors and analog devices it will be useful


    Tinkercad

    Tinkercad is a free, web-based platform by Autodesk for 3D design, circuit simulation, and Arduino coding. It allows users to create 3D models, simulate electronic circuits, and program Arduino using block or text-based coding. With a user-friendly interface, it is ideal for beginners in electronics, robotics, and design. No installation is needed, as it runs directly in a web browser.

    I used Tinkercad for basic Arduino simulations. I explored the interface of tinkercad and got some ideas how to do the Components arrangements, connections btw the others, coding and simulations.

    LED blink

    First i did the basic simulation of LED blink, I take the arduino in the library and also collect the LED and resistor, Connect them as per the code which is I given in the Coding interface.

    void setup()
    {
      pinMode(LED_BUILTIN, OUTPUT);
    }
    void loop()
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000); 
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000); 
    }

    • void setup() is a function, whenever the Arduino is powered on or reset it will run. It is used to set the initial configurations like pin modes.

    • In pinMode we can set the LED pin as an output.

    • void loop() is a function which can runs repeatedly in a loop after one another

    • By using the digitalWrite we can turns on & off the LED by sending a HIGH signal (5V) and LOW signal (0V) to the LED pin.

    • delay() function is used to describe our works within the time frame, here I used the 1000 milliseconds, after every that seconds LED will blink.


    LCD Display

    After that I do the simulations with utilize the LCD display,

    Adafruit_LiquidCrystal lcd(0);
    
    void setup()
    {
      lcd.begin(16, 2);
      lcd.setCursor(1,0);
      lcd.print("Fablab  Trichy");
    }
    void loop()
    {
      lcd.setBacklight(1);
      delay(100); 
      lcd.setBacklight(0);
      delay(10);   
    }

    • Here I used the Adafruit_LiquidCrystal library to control the LCD display

    • In lcd.begin we should initializes the LCD with 16 columns and 2 rows like what type of Display is that.

    • I set the lcd.setCursor as the cursor position to column 1, row 0.

    • To display the text "Fablab Trichy" we can use the lcd.print starting from the current cursor position on the LCD.

    • For blinking the display I set the lcd.setBacklight() 1 &0 for turn on and off the LCD display light.


    Servo mechanism

    Further I did the Servo actuation by controlling the code as well

    #include <Servo.h>
    int pos = 0;
    Servo servo_10;
    void setup()
    {
      servo_10.attach(2, 500, 2500);
    }
    void loop()
    {
      for (pos = 0; pos <= 90; pos += 1) {
        servo_10.write(pos);
        delay(1); 
      }
    }

    • I included the<Servo.h> to control the servo motor

    • To declares an integer variable named as pos and initializes it to 0.

    • In servo_10.attach(2, 500, 2500) we can set the minimum and maximum pulse widths (in microseconds). 2 is the Digital pin and where 500 and 2500 refers 500 Ξs ≈ 0°, 2500 Ξs ≈ 180°.


    ESP

    ESP (Espressif Systems) is a series of low-power microcontrollers widely used in IoT applications. It offers built-in Wi-Fi and Bluetooth connectivity, making it ideal for wireless communication. ESP modules support multiple programming environments, including Arduino, MicroPython, and ESP-IDF. They are known for their high performance, low power consumption, and cost-effectiveness. ESP microcontrollers integrate various peripherals such as ADC, DAC, PWM, and GPIOs. They are commonly used in smart home devices, automation, and embedded systems. The most popular models include ESP8266 and ESP32, with ESP32 offering enhanced features like dual-core processing.

    I explored ESP32 microcontrollers and gained knowledge that may make it a suitable choice for my final project.

    ESP32

    The ESP32 is a powerful Wi-Fi and Bluetooth-enabled microcontroller developed by Espressif Systems. It features a dual-core 32-bit Xtensa LX6 processor, running up to 240 MHz, with 520 KB SRAM and 4 MB flash memory (varies by model). It supports Wi-Fi (802.11 b/g/n), Bluetooth 4.2/BLE, GPIOs, ADC, DAC, SPI, I2C, UART, and PWM. Known for its low power consumption and high performance, it is widely used in IoT, robotics, automation, and embedded systems.

    Specification

    • Architecture: 32-bit Xtensa LX6 (dual-core)
    • Clock Speed: Up to 240 MHz
    • Flash Memory: Up to 16 MB (varies by model)
    • SRAM: 520 KB (internal)
    • GPIO Pins: Up to 34 I/O pins
    • ADC: 18 channels (12-bit resolution)
    • DAC: 2 channels (8-bit resolution)
    • Communication Interfaces: UART, SPI, I2C, I2S, CAN, PWM
    • Wireless Connectivity: Wi-Fi (802.11 b/g/n), Bluetooth 4.2 + BLE
    • Operating Voltage: 3.3V

    Datasheet

    ESP32 Block Diagram

    I explored the ESP32 block diagram section.
    The block diagram shows how different internal components are connected inside the microcontroller. For example, the CPU communicates with memory, peripherals, and wireless modules to execute programs and process data.

    The block diagram helps me understand the internal architecture of the ESP32 and how different subsystems work together.

    GPIO and Pin Functions

  • Digital input/output pins
  • Alternate pin functions
  • Input and output capabilities
  • Image source

    It helps identify which pins can be used for sensors, actuators, and communication interfaces.

    Memory Organization

  • SRAM for runtime data
  • Flash memory for program storage
  • ROM for boot operations
  • When an ESP32 starts up, it first executes code stored in ROM to initialize the system. The user program is stored in Flash memory, and during execution, variables, sensor readings, and temporary data are stored in SRAM.
    Understanding memory organization helps me know where code and data are stored inside the microcontroller. It also helps in optimizing memory usage and troubleshooting issues related to program storage and runtime memory limitations.

    Wi-Fi and Bluetooth Features

  • Wi-Fi 802.11 b/g/n support
  • Bluetooth Classic
  • Bluetooth Low Energy (BLE)
  • The ESP32 can connect to a wireless network and exchange data with computers, smartphones, or cloud services.

    ADC Section

  • 12-bit ADC resolution
  • Multiple ADC channels
  • Configurable input ranges
  • A 12-bit ADC converts analog voltages into digital values ranging from 0 to 4095, allowing more precise measurements than a 10-bit ADC.
    It helps in reading analog signals from sensors and measuring varying voltage levels accurately.

    Communication Interfaces

    I explored the Communication Interfaces section of the ESP32 datasheet. This section describes how the ESP32 exchanges data with external devices and other microcontrollers.
    UART (Universal Asynchronous Receiver Transmitter)
    UART is a serial communication protocol that uses two wires:
  • TX (Transmit)
  • RX (Receive)
  • UART can be used to send sensor data from the ESP32 to a computer through the Serial Monitor or communicate with modules such as GPS receivers.
    SPI (Serial Peripheral Interface)
    SPI is a high-speed communication protocol that uses:
  • MOSI (Master Out Slave In)
  • MISO (Master In Slave Out)
  • SCK (Clock)
  • CS (Chip Select)
  • SPI is commonly used to communicate with SD card modules, TFT displays, and external memory devices where fast data transfer is required.
    I2C (Inter-Integrated Circuit)
    I2C is a communication protocol that uses only two wires:
  • SDA (Data)
  • SCL (Clock)
  • Multiple sensors such as OLED displays, temperature sensors, and RTC modules can be connected to the same I2C bus while using only two communication pins.
    I2S (Inter-IC Sound)
    I2S is a digital audio communication protocol.
    It is used to connect microphones, audio codecs, and speakers for audio processing applications.
    By these we learned how the ESP32 can communicate with sensors, displays, memory modules, audio devices, and other microcontrollers in embedded system applications.


    Seeed studio

    Seeed Studio is a hardware innovation platform that provides open-source electronic modules, development boards, and IoT solutions. It offers products like Seeeduino (Arduino-compatible boards), Grove sensors, XIAO microcontrollers, and Edge AI solutions. Known for its rapid prototyping, industrial IoT, and AI hardware, Seeed Studio supports makers, students, and engineers in building embedded systems and smart devices.


    Furthermore, I explored this in greater depth and practiced working with it.

    XIAO-ESP32-C3

    The Seeed Studio XIAO ESP32-C3 is a compact Wi-Fi + Bluetooth 5 (BLE) microcontroller based on the ESP32-C3 (RISC-V single-core CPU @160 MHz). It features 4 MB Flash, 400 KB SRAM, and supports GPIO, ADC, PWM, UART, SPI, and I2C. With its small size (21×17.5 mm), low power consumption, and Type-C USB, it is ideal for IoT, wearables, and edge computing applications.

    Specification

    • Architecture: 32-bit RISC-V (single-core)
    • Architecture: 32-bit RISC-V (single-core)
    • Clock Speed: Up to 160 MHz
    • Flash Memory: 4 MB
    • SRAM: 400 KB
    • GPIO Pins: 11 I/O pins (supports ADC, PWM, I2C, SPI, UART)
    • Wireless Connectivity: Wi-Fi (802.11 b/g/n, 2.4 GHz), Bluetooth 5 (BLE)
    • USB Interface: USB Type-C (native)
    • Operating Voltage: 3.3V
    • Dimensions: 21 × 17.5 mm

    Datasheet

    Image source

    Wi-Fi and Bluetooth Low Energy (BLE)

    I explored the wireless communication section of the datasheet.
  • 2.4 GHz Wi-Fi (802.11 b/g/n)
  • Bluetooth 5 Low Energy (BLE)
  • The ESP32-C3 can transmit sensor data wirelessly to smartphones, computers, or cloud platforms without requiring additional communication modules.
    It helps me understand the wireless capabilities available for IoT and remote monitoring applications.

    USB Serial/JTAG Controller

    I found that the ESP32-C3 includes a built-in USB Serial/JTAG controller.
    The microcontroller can be programmed and debugged directly through the USB Type-C connection without requiring an external USB-to-UART converter.
    This simplifies programming, debugging, and firmware uploading during embedded system development.

    GPIO Matrix and Peripheral Routing

    I found that many peripheral signals can be mapped to different GPIO pins through the GPIO Matrix.
    Communication peripherals such as UART, SPI, and I2C are not always fixed to specific pins and can often be assigned to different GPIOs.
    This provides greater flexibility when designing custom PCBs and connecting external devices.

    Security Features

  • Secure Boot
  • Flash Encryption
  • Secure Boot ensures that only trusted firmware can run on the microcontroller.
    These features help protect embedded systems from unauthorized firmware modifications and improve device security.

    Low-Power Features

    Multiple power-saving modes designed to reduce energy consumption. The ESP32-C3 can enter sleep modes when inactive and wake up when an event occurs.
    This is important for battery-powered IoT devices where power efficiency is critical.


    Wokwi

    Wokwi is a web-based electronics simulator for Arduino, ESP32, Raspberry Pi, and AVR microcontrollers. It allows users to write, test, and debug embedded code in real time without physical hardware. The platform supports sensors, displays, motors, and communication modules. It is ideal for IoT, robotics, and embedded systems development.

    Using this, I conducted two simulations with different boards: ESP32 and XIAO-ESP32-C3.

    ESP32 with OLED display

    I used an OLED display to obtain output with the ESP32, programming it in MicroPython.

    from machine import Pin, I2C
    import ssd1306
    
    # ESP32 Pin assignment 
    i2c = I2C(0, scl=Pin(22), sda=Pin(21))
    
    oled_width = 128
    oled_height = 64
    oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
    
    oled.text('VANAKAM', 35, 25)      
    oled.show()

    • I Imports the ssd1306 library to control SSD1306 OLED displays.

    • In i2c we should set the Pins for scl and sda , scl is the clock pin and sda is the dat pin

    • Further I set the oled display height and width.

    • In oled.text we will show the text and set the dimension as well


    XIAO-ESP32-C3 with LED blink

    I also experimented with LED blinking using this microcontroller.

    void setup() {
      Serial.begin(115200);
      pinMode(D2, OUTPUT);
      pinMode(D3, OUTPUT);
      pinMode(D4, OUTPUT);
      Serial.println("");
      Serial.println("Let's Start");
    }
    void loop() {
      Serial.println("Red");
      digitalWrite(D2, HIGH);
      delay(100);
      digitalWrite(D2, LOW);
      Serial.println("Yellow");
      digitalWrite(D3, HIGH);
      delay(100);
      digitalWrite(D3, LOW);
      Serial.println("Green");
      digitalWrite(D4, HIGH);
      delay(100);
      digitalWrite(D4, LOW);
    }

    • By using the Serial.begin line in Arduino programming we can initialize serial communication between the Arduino board and our computer.

    • After I set the pinMode() for the LED connections

    • We can start the process in serial monitor, I set the Serial.println to display the led blink colour in serial monitor

    • Set the digitalWrite for operate the led which can be function and when it should be.


    I2C communication


    To program a physical development board using an embedded programming language to communicate and interact with devices, I plan to do by using the potentiometer control the moverment of the Motor.

    For this I used I2C communication between two microcontrollers.

    I took the Arduino UNo and Xiao nrf52840 microcontrollers, here the Arduino Uno acts as the MASTER device and the Seeed Studio XIAO nRF52840 acts as the SLAVE device.
    The potentiometer is connected with Arduino UNO and the MG996R is connected with XIAO nRF52840.
    When I rotate the potentiometer knob, the Arduino reads the analog value from it. The potentiometer gives values from 0 to 1023.
    In I2C communication: 1.SDA line is used for data transfer 2.SCL line is used for clock synchronization

    Arduino UNO code

    
    #include <Wire.h>
    void setup() {
      Wire.begin(); // Master
    }
    void loop() {
      int potValue = analogRead(A0);
      byte angle = map(potValue, 0, 1023, 0, 180);
      Wire.beginTransmission(8); // Slave address
      Wire.write(angle);
      Wire.endTransmission();
      delay(20);
    }

    • #include <Wire.h> is used to include the I2C communication library.

    • Wire.begin(); initializes the Arduino for I2C communication as Master.

    • The potentiometer gives values between 0 to 1023.

    • Wire.beginTransmission(8) starts communication with the XIAO board having slave address 8.

    • Wire.write(angle) sends the angle value through I2C communication.

    • Wire.endTransmission() stops the data transmission process.

    Xiao-nRF52840 code

    
    #include <Wire.h>
    #include <Servo.h>
    
    Servo myServo;
    
    volatile int receivedAngle = 0;
    
    void receiveEvent(int howMany) {
    
      while (Wire.available()) {
        receivedAngle = Wire.read();
      }
    }
    
    void setup() {
    
      myServo.attach(D2);
    
      Wire.begin(8); // Slave address
    
      Wire.onReceive(receiveEvent);
    }
    
    void loop() {
    
      myServo.write(receivedAngle);
    
      delay(10);
    }
    }

    • #include <Servo.h> is used to control the MG996R.

    • Servo myServo; creates a servo motor object for controlling the servo.

    • myServo.attach(D2); connects the servo signal wire to pin D2.

    • Wire.begin(8); initializes the XIAO board as SLAVE with address 8.

    • Wire.onReceive(receiveEvent); calls the receiveEvent() function whenever data comes from Arduino.

    • myServo.write(receivedAngle); rotates the servo motor according to the received angle value.

    I established I2C communication between the Arduino Uno and Seeed Studio XIAO nRF52840.
    The Arduino reads the potentiometer value and converts it into a servo angle. Then the angle data is transmitted to the XIAO board through I2C protocol. The XIAO board receives the data and controls the MG996R accordingly.
    Finally, the servo motor rotates based on the potentiometer knob position.
    I used Embedded C++ programming language for this work.
    I used Arduino IDE for writing, compiling, and uploading the code. This language is simple for hardware interfacing such as I2C communication, analog input reading, and servo motor control.
    I selected Arduino IDE because it provides built-in board support, library support, simple code uploading, and easy debugging for both development boards.

    Datasheet acknowledgement

    While developing this I2C communication project, I applied several concepts that I learned from exploring the microcontroller datasheets.
    ADC (Analog-to-Digital Converter) The potentiometer produces an analog voltage, which is read by the Arduino Uno through its ADC module. The ADC converts the analog signal into a digital value ranging from 0 to 1023, allowing the microcontroller to process the potentiometer position.
    I2C Communication Interface I used the I2C communication peripheral available in both microcontrollers. The Arduino Uno acts as the Master device and the XIAO nRF52840 acts as the Slave device. The SDA and SCL lines are used to transfer data and synchronize communication between the boards.
    GPIO (General Purpose Input/Output) The potentiometer was connected to an analog input pin of the Arduino Uno, while the servo signal wire was connected to a GPIO pin of the XIAO nRF52840 for motor control.


    Conclusion

    Learning outcomes

    • Learn about how to browse the datasheet for our microcontroller

    • What are the pins will avialble in some microcontrollers and those functions and uses

    • To learn about the TinkerCAD and WOKWI simulators to how we can use these simulators for our codings with some basic input and output devices.

    • Little bit know about the Arduio and Micropython code, how it operate, how we can use it effciently, so hope I will get strong knowledge in future.


    Reference Files

    Here is my codes